<?php
/**
 * @Copyright
 * @package    	EasyCalcCheck Plus
 * @author		Viktor Vogel {@link http://www.kubik-rubik.de}
 * @link		Project Site {@link http://joomla-extensions.kubik-rubik.de/ecc-easycalccheck-plus}
 */
 
// Kein direkter Zugriff
defined('_JEXEC') or die('Restricted access');

$session = JFactory::getSession();

if ($option == 'com_contact')
{
	if (JRequest::getCmd('essp_err', '', 'get') == 'check_failed')
	{
		$name = $session->get('autofill_name', null, 'easycalccheck');
		if (!empty($name))
		{
			$pattern = '@(<input[^>]+name="name".+(value="").*/>)@isU';
			if (preg_match($pattern, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$name.'"', $matches[1]);
				$body = preg_replace($pattern, $replace_value, $body);
			}
		}
		$email = $session->get('autofill_email', null, 'easycalccheck');
		if (!empty($email))
		{
			$pattern = '@(<input type="text" id="contact-email" name="email".+(value="").*/>)@isU';
			if (preg_match($pattern, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$email.'"', $matches[1]);
				$body = preg_replace($pattern, $replace_value, $body);
			}
		}
		$subject = $session->get('autofill_subject', null, 'easycalccheck');
		if (!empty($subject))
		{
			$pattern = '@(<input[^>]+name="subject".+(value="").*/>)@isU';
			if (preg_match($pattern, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$subject.'"', $matches[1]);
				$body = preg_replace($pattern, $replace_value, $body);
			}
		}
		$text = $session->get('autofill_text', null, 'easycalccheck');
		if (!empty($text))
		{
			$pattern = '@(<textarea[^>]+name="text".+>(</textarea>))@isU';
			if (preg_match($pattern, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', $text.'</textarea>', $matches[1]);
				$body = preg_replace($pattern, $replace_value, $body);
			}
		}
	}
	$session->clear('autofill_name','easycalccheck');
	$session->clear('autofill_email','easycalccheck');
	$session->clear('autofill_subject','easycalccheck');
	$session->clear('autofill_text','easycalccheck');
	$session->clear('autofill_body','easycalccheck');
	$session->clear('autofill_message','easycalccheck');
}

if ($option == 'com_users')
{
	if (JRequest::getCmd('essp_err', '', 'get') == 'check_failed')
	{
		$name = $session->get('autofill_name', null, 'easycalccheck');
		if (!empty($name))
		{
			$pattern = '@(<input[^>]+name="jform\[name\]".+(value=".*").*/>)@isU';
			if (preg_match($pattern, $body, $matches))
			{
				$replace_value = str_replace($matches[2], 'value="'.$name.'"', $matches[1]);
				$body = preg_replace($pattern, $replace_value, $body);
			}
		}
		$username = $session->get('autofill_username', null, 'easycalccheck');
		if (!empty($username))
		{
			$pattern = '@(<input[^>]+name="jform\[username\]".+(value=".*").*/>)@isU';
			if (preg_match($pattern, $body, $matches))
			{
				$replace_value = str_replace($matches[2], 'value="'.$username.'"', $matches[1]);
				$body = preg_replace($pattern, $replace_value, $body);
			}
		}
		$email1 = $session->get('autofill_email1', null, 'easycalccheck');
		if (!empty($email1))
		{
			$pattern = '@(<input[^>]+name="jform\[email1\]".+(value=".*").*/>)@isU';
			if (preg_match($pattern, $body, $matches))
			{
				$replace_value = str_replace($matches[2], 'value="'.$email1.'"', $matches[1]);
				$body = preg_replace($pattern, $replace_value, $body);
			}
		}
		$email2 = $session->get('autofill_email2', null, 'easycalccheck');
		if (!empty($email2))
		{
			$pattern = '@(<input[^>]+name="jform\[email2\]".+(value=".*").*/>)@isU';
			if (preg_match($pattern, $body, $matches))
			{
				$replace_value = str_replace($matches[2], 'value="'.$email2.'"', $matches[1]);
				$body = preg_replace($pattern, $replace_value, $body);
			}
		}
		// Clear password values - password has alway to be entered new
		$pattern = '@(<input[^>]+name="jform\[password1\]".+(value=".*").*/>)@isU';
		if (preg_match($pattern, $body, $matches))
		{
			$replace_value = str_replace($matches[2], 'value=""', $matches[1]);
			$body = preg_replace($pattern, $replace_value, $body);
		}
		$pattern = '@(<input[^>]+name="jform\[password2\]".+(value=".*").*/>)@isU';
		if (preg_match($pattern, $body, $matches))
		{
			$replace_value = str_replace($matches[2], 'value=""', $matches[1]);
			$body = preg_replace($pattern, $replace_value, $body);
		}
		
	}
	$session->clear('autofill_name','easycalccheck');
	$session->clear('autofill_username','easycalccheck');
	$session->clear('autofill_email1','easycalccheck');
	$session->clear('autofill_email2','easycalccheck');
}

if ($option == 'com_easybookreloaded')
{
	if (JRequest::getCmd('essp_err', '', 'get') == 'check_failed')
	{
		$user = JFactory::getUser();
		if ($user->guest) // Re-enter name and e-mail address only for guests
		{
			$gbname = $session->get('autofill_gbname', null, 'easycalccheck');
			if (!empty($gbname))
			{
				$muster = '@(<input[^>]+name=\'gbname\'.+(value=\'\').*/>)@isU';
				if (preg_match($muster, $body, $matches))
				{
					$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbname.'"', $matches[1]);
					$body = preg_replace($muster, $replace_value, $body);
				}
			}
			$gbmail = $session->get('autofill_gbmail', null, 'easycalccheck');
			if (!empty($gbmail))
			{
				$muster = '@(<input[^>]+name=\'gbmail\'.+(value=\'\').*/>)@isU';
				if (preg_match($muster, $body, $matches))
				{
					$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbmail.'"', $matches[1]);
					$body = preg_replace($muster, $replace_value, $body);
				}
			}
		}
		$gbpage = $session->get('autofill_gbpage', null, 'easycalccheck');
		if (!empty($gbpage))
		{
			$muster = '@(<input[^>]+name=\'gbpage\'.+(value=\'\').*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbpage.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$gbloca = $session->get('autofill_gbloca', null, 'easycalccheck');
		if (!empty($gbloca))
		{
			$muster = '@(<input[^>]+name=\'gbloca\'.+(value=\'\').*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbloca.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$gbicq = $session->get('autofill_gbicq', null, 'easycalccheck');
		if (!empty($gbicq))
		{
			$muster = '@(<input[^>]+name=\'gbicq\'.+(value=\'\').*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbicq.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$gbaim = $session->get('autofill_gbaim', null, 'easycalccheck');
		if (!empty($gbaim))
		{
			$muster = '@(<input[^>]+name=\'gbaim\'.+(value=\'\').*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbaim.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$gbmsn = $session->get('autofill_gbmsn', null, 'easycalccheck');
		if (!empty($gbmsn))
		{
			$muster = '@(<input[^>]+name=\'gbmsn\'.+(value=\'\').*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbmsn.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$gbyah = $session->get('autofill_gbyah', null, 'easycalccheck');
		if (!empty($gbyah))
		{
			$muster = '@(<input[^>]+name=\'gbyah\'.+(value=\'\').*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbyah.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$gbskype = $session->get('autofill_gbskype', null, 'easycalccheck');
		if (!empty($gbskype))
		{
			$muster = '@(<input[^>]+name=\'gbskype\'.+(value=\'\').*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$gbskype.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$gbvote = $session->get('autofill_gbvote', null, 'easycalccheck');
		if (!empty($gbvote))
		{
			$body = str_replace('<option>'.$gbvote.'</option>', '<option selected="selected">'.$gbvote.'</option>', $body);
		}
		$gbtext = $session->get('autofill_gbtext', null, 'easycalccheck');
		if (!empty($gbtext))
		{
			$muster = '@(<textarea[^>]+name=\'gbtext\'.+>(</textarea>))@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', $gbtext.'</textarea>', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
	}
	$session->clear('autofill_gbname','easycalccheck');
	$session->clear('autofill_gbtext','easycalccheck');
	$session->clear('autofill_gbmail','easycalccheck');
	$session->clear('autofill_gbpage','easycalccheck');
	$session->clear('autofill_gbloca','easycalccheck');
	$session->clear('autofill_gbicq','easycalccheck');
	$session->clear('autofill_gbaim','easycalccheck');
	$session->clear('autofill_gbmsn','easycalccheck');
	$session->clear('autofill_gbyah','easycalccheck');
	$session->clear('autofill_gbskype','easycalccheck');
	$session->clear('autofill_gbvote','easycalccheck');
}

if ($option == 'com_phocaguestbook')
{
	if (JRequest::getCmd('essp_err', '', 'get') == 'check_failed' AND $_SESSION["phocaguestbook"] == 1)
	{
		$title = $session->get('autofill_title', null, 'easycalccheck');
		if (!empty($title))
		{
			$muster = '@(<input[^>]+name="title".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$title.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$pgusername = $session->get('autofill_pgusername', null, 'easycalccheck');
		if (!empty($pgusername))
		{
			$muster = '@(<input[^>]+name="pgusername".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$pgusername.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$email = $session->get('autofill_email', null, 'easycalccheck');
		if (!empty($email))
		{
			$muster = '@(<input[^>]+name="email".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$email.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$pgbcontent = $session->get('autofill_pgbcontent', null, 'easycalccheck');
		if (!empty($pgbcontent))
		{
			$muster = '@(<textarea[^>]+name="pgbcontent".+>(</textarea>))@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', $pgbcontent.'</textarea>', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
	}
	$session->clear('autofill_title','easycalccheck');
	$session->clear('autofill_pgusername','easycalccheck');
	$session->clear('autofill_email','easycalccheck');
	$session->clear('autofill_pgbcontent','easycalccheck');
}

if ($option == 'com_comprofiler')
{
	if (JRequest::getCmd('essp_err', '', 'get') == 'check_failed')
	{
		$name = $session->get('autofill_name', null, 'easycalccheck');
		if (!empty($name))
		{
			$muster = '@(<input[^>]+name="name".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$name.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$firstname = $session->get('autofill_firstname', null, 'easycalccheck');
		if (!empty($firstname))
		{
			$muster = '@(<input[^>]+name="firstname".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$firstname.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$middlename = $session->get('autofill_middlename', null, 'easycalccheck');
		if (!empty($middlename))
		{
			$muster = '@(<input[^>]+name="middlename".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$middlename.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$lastname = $session->get('autofill_lastname', null, 'easycalccheck');
		if (!empty($lastname))
		{
			$muster = '@(<input[^>]+name="lastname".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$lastname.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$email = $session->get('autofill_email', null, 'easycalccheck');
		if (!empty($email))
		{
			$muster = '@(<input[^>]+name="email".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$email.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$username = $session->get('autofill_username', null, 'easycalccheck');
		if (!empty($username))
		{
			$muster = '@(<input[^>]+name="username".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$username.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$company = $session->get('autofill_company', null, 'easycalccheck');
		if (!empty($company))
		{
			$muster = '@(<input[^>]+name="company".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$company.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$city = $session->get('autofill_city', null, 'easycalccheck');
		if (!empty($city))
		{
			$muster = '@(<input[^>]+name="city".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$city.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$state = $session->get('autofill_state', null, 'easycalccheck');
		if (!empty($state))
		{
			$muster = '@(<input[^>]+name="state".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$state.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$zipcode = $session->get('autofill_zipcode', null, 'easycalccheck');
		if (!empty($zipcode))
		{
			$muster = '@(<input[^>]+name="zipcode".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$zipcode.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$country = $session->get('autofill_country', null, 'easycalccheck');
		if (!empty($country))
		{
			$muster = '@(<input[^>]+name="country".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$country.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$address = $session->get('autofill_address', null, 'easycalccheck');
		if (!empty($address))
		{
			$muster = '@(<input[^>]+name="address".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$address.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$phone = $session->get('autofill_phone', null, 'easycalccheck');
		if (!empty($phone))
		{
			$muster = '@(<input[^>]+name="phone".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$phone.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
		$fax = $session->get('autofill_fax', null, 'easycalccheck');
		if (!empty($fax))
		{
			$muster = '@(<input[^>]+name="fax".+(value="").*/>)@isU';
			if (preg_match($muster, $body, $matches))
			{
				$replace_value = preg_replace('@'.$matches[2].'@', 'value="'.$fax.'"', $matches[1]);
				$body = preg_replace($muster, $replace_value, $body);
			}
		}
	}
	$session->clear('autofill_name','easycalccheck');
	$session->clear('autofill_firstname','easycalccheck');
	$session->clear('autofill_middlename','easycalccheck');
	$session->clear('autofill_lastname','easycalccheck');
	$session->clear('autofill_email','easycalccheck');
	$session->clear('autofill_username','easycalccheck');
	$session->clear('autofill_company','easycalccheck');
	$session->clear('autofill_city','easycalccheck');
	$session->clear('autofill_state','easycalccheck');
	$session->clear('autofill_zipcode','easycalccheck');
	$session->clear('autofill_country','easycalccheck');
	$session->clear('autofill_address','easycalccheck');
	$session->clear('autofill_phone','easycalccheck');
	$session->clear('autofill_fax','easycalccheck');
}
JResponse::setBody($body);
?>